package java0709Exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TestCh9 {

	public static void main(String[] args) {

		// / by zero 异常信息 可逻辑解除，或trycatch 保证主程序运行
		//test01();
		// Exception in thread "main" java.lang.ArithmeticException: / by zero
		// at java0709Exception.TestCh9.test01(TestCh9.java:18)
		// at java0709Exception.TestCh9.main(TestCh9.java:9)

		System.out.println("================");
		test02();
		// ================
		// j=1
		// 程序运行结束
		
		System.out.println("================");
		test03();
		// ================
		// 发生异常：/ by zero
		// 程序运行结束
		// java.lang.ArithmeticException: / by zero
		// at java0709Exception.TestCh9.test03(TestCh9.java:26)
		// at java0709Exception.TestCh9.main(TestCh9.java:17)
		
		try {
		test04();
		
		}catch(FileNotFoundException e) {
			System.out.println("抛出异常");
			e.printStackTrace();
		}finally {
			System.out.println("执行finally异常");
		}
		
		try {
		test05();
		}catch(IllegalAgeException e) {
			System.out.println("catch块处理");
			
		}finally {
			System.out.println("finally块");
		}
		}
	
	
	private static void test05() throws IllegalAgeException{
		int age = 1000;
		if(age>200) {
			throw new IllegalAgeException("发生异常");
			
		}
	}
	
	
	
	private static void test04() throws FileNotFoundException {
		
		FileInputStream fis = new FileInputStream("d:/demo/test04.txt");
		
		
	}
	
	
	

	/**
	 * try catch 保证主程序运行 运行时异常 除数为零
	 */
	private static void test03() {
		int i = 0;
		try {
		int j = 10 / i;
		System.out.println("j=" + j);
		}catch(ArithmeticException e){
			System.out.println("发生异常："+e.getMessage());
			e.printStackTrace();
		}finally {
			System.out.println("执行了finally语句");
		}
		System.out.println("程序运行结束");
	}

	/**
	 * 逻辑解除 运行时异常 除数为零
	 */
	private static void test02() {
		int i = 0;
		int j;
		if (i != 0) {
			j = 10 / i;
		} else {
			j = 1;
		}
		System.out.println("j=" + j);
		System.out.println("程序运行结束");
	}

	/**
	 * 运行时异常 除数为零
	 */
	private static void test01() {
		int i = 0;
		int j = 10 / i;
		System.out.println("j=" + j);
		System.out.println("程序运行结束");
	}
}
